package dtnnews.views;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.example.dtnnews.R;

import dtnews.getdata.AndroidDataFactory;
import dtnews.getdata.AndroidGetData;
import dtnews.getdata.ConvertTools;
import dtnnews.models.News;
import dtnnews.supports.Constant;
import dtnnews.supports.ListNewsAdapter;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;


public class PageMain extends Activity {

	//Layout
	LinearLayout layoutTitle = null;
	
	//Button
	Button btnNews = null;
	Button btnCareer = null;
	Button btnScience = null;
	Button btnIntroduction = null;
	Button btnShorten = null;
	
	//Other controls
	ListView lstView = null;
	ImageView imgTitle = null;
	
	//Other attributes
	ListNewsAdapter adapter = null;
	ArrayList<News> lstNews = null;
	boolean isShorten = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	
		//Lấy luồng cung cấp internet
		ThreadPolicy tp = ThreadPolicy.LAX;
		StrictMode.setThreadPolicy(tp);
	    lstNews = new ArrayList<News>();
		Thread thread = new Thread() {
			public void run() {
				try {
					lstNews = ConvertTools.convertToListNews(AndroidDataFactory.getListNewsPreview(AndroidDataFactory.KHOA_HOC));
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				}
				
			}
		};
		
		thread.start();

		//Lấy địa chỉ các control
		layoutTitle = (LinearLayout) findViewById(R.id.layout_title);
		
		//Ảnh tiêu đề
		imgTitle = new ImageView(this);
		imgTitle.setBackgroundResource(R.drawable.bgr_title);
		imgTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 135));
				
		btnShorten = (Button) findViewById(R.id.btn_shorten);
		btnShorten.setOnClickListener(btnShortenClickListener);
		
		btnNews = (Button) findViewById(R.id.btn_news);
		btnNews.setOnClickListener(btnNewsClickListener);
		
		btnCareer = (Button) findViewById(R.id.btn_career);
		btnCareer.setOnClickListener(btnCareerClickListener);
		
		btnScience = (Button) findViewById(R.id.btn_science);
		btnScience.setOnClickListener(btnScienceClickListener);
		
		btnIntroduction = (Button) findViewById(R.id.btn_introduction);
		btnIntroduction.setOnClickListener(btnIntroductionClickListener);
		
		
		
		TextView test = (TextView) findViewById(R.id.address);
		test.setText(Constant.testValue);
		
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return false;
	}
	

	//======================KHỐI ĐỊNH NGHĨA CÁC HÀM HỖ TRỢ==================
	
	//Sự kiện nút bấm tin tức
	private OnClickListener btnNewsClickListener = new OnClickListener() {

		
		@Override
		public void onClick(View v) {
			adapter = new ListNewsAdapter(lstNews, PageMain.this);
			lstView = (ListView) findViewById(R.id.list_view);
			lstView.setOnItemClickListener(lstViewClickListener);
			lstView.setAdapter(adapter);
			
		}
		
	};
	
	//Sự kiện nút bấm việc làm
	private OnClickListener btnCareerClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			
			
		}
		
	};
	
	//Sự kiện nút bấm khoa học
	private OnClickListener btnScienceClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			
			
		}
		
	};
	
	//Sự kiện nút bấm giới thiệu
	private OnClickListener btnIntroductionClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			
			
		}
		
	};
	
	//Sự kiện nút bấm thu gọn
	private OnClickListener btnShortenClickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			isShorten = !isShorten;
			
			//Chuyển image và hiện ẩn khung tiêu đề khi click 
			if(isShorten) {
				layoutTitle.removeAllViews();
				btnShorten.setBackgroundResource(R.drawable.btn_shorten_down);
			}
			else {
				layoutTitle.addView(imgTitle);
				btnShorten.setBackgroundResource(R.drawable.btn_shorten_up);
			}
		}
		
	};
	
	//Sự kiện chọn tronh listview
	private OnItemClickListener lstViewClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> adapter, View view, int pos, 
				long id) {
			try {
				String viewUrl = lstNews.get(pos).getViewUrl();
				String content = AndroidGetData.getDetailHTMLContent(viewUrl);
				Intent i = new Intent(PageMain.this,PageDetail.class);
				i.putExtra(Constant.EXTRA_URL_VIEW, content);
				startActivity(i);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	};
	
	

}
